﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConanLib
{
    public class ColorMapper
    {
        public Dictionary<string, System.Drawing.Color> Mapping { get; private set; }


        private List<string> _knownColors = new List<string>();
        public ColorMapper()
        {
            foreach (var item in Enum.GetValues(typeof(System.Drawing.KnownColor)))
            {
                _knownColors.Add(item.ToString().ToLower());
            }
        }

        private int lastColorIndex = 1; //exclude Black by starting at 1 
        public System.Drawing.Color Map(string value)
        {
             if (Mapping.ContainsKey(value))
                return Mapping[value];


            //lets see whether its a known color
             if (_knownColors.Contains(value))
             {
                 Mapping.Add(value, System.Drawing.Color.FromName(value));
                 return Mapping[value];
             }
            

            //lets see whehter we have a rdgb
            string[] rgb = value.Split(",".ToCharArray(),  StringSplitOptions.RemoveEmptyEntries);
            if (rgb.Length  == 3)
            {
                int r;
                int g;
                int b;
                if (int.TryParse(rgb[0], out r) && int.TryParse(rgb[1], out g) && int.TryParse(rgb[2], out b))
                {
                    if (r >= 0 && r <= 255 && g >= 0 && g <= 255 && b >= 0 && b <= 255)
                    {
                        Mapping.Add(value, System.Drawing.Color.FromArgb(r, g, b));
                        return Mapping[value];
                    }
                }
            }

            //if nothibng helps use predefiend colors
            Mapping.Add(value, colors[lastColorIndex]);
            if (lastColorIndex < colors.Length - 1)
                lastColorIndex++;
            else
                lastColorIndex = 1; //exclude Black by starting at 1 
            return Mapping[value];
        }

        public void Init()
        {
            Mapping = new Dictionary<string, System.Drawing.Color>();

            System.Drawing.ColorConverter colorConverter = new System.Drawing.ColorConverter();
            //colors = new System.Drawing.Color[7];
            //colors[0] = (System.Drawing.Color)colorConverter.ConvertFrom("Blue");
            //colors[1] = (System.Drawing.Color)colorConverter.ConvertFrom("Red");
            //colors[2] = (System.Drawing.Color)colorConverter.ConvertFrom("Green");
            //colors[3] = (System.Drawing.Color)colorConverter.ConvertFrom("Orange");
            //colors[4] = (System.Drawing.Color)colorConverter.ConvertFrom("Brown");
            //colors[5] = (System.Drawing.Color)colorConverter.ConvertFrom("Purple");
            //colors[6] = (System.Drawing.Color)colorConverter.ConvertFrom("Pink"); 
            colors = new System.Drawing.Color[30];
            colors[0] = System.Drawing.Color.FromArgb(255, 0, 0, 0);

            colors[1] = System.Drawing.Color.FromArgb(255, 255, 0, 0);
            colors[2] = System.Drawing.Color.FromArgb(255, 0, 255, 0);
            colors[3] = System.Drawing.Color.FromArgb(255, 0, 0, 255);

            //colors[4] = System.Drawing.Color.FromArgb(255, 255, 255, 0); //no yellow please
            colors[4] = System.Drawing.Color.FromArgb(255, 0, 255, 255);
            colors[5] = System.Drawing.Color.FromArgb(255, 255, 0, 255);

            colors[6] = System.Drawing.Color.FromArgb(255, 127, 0, 127);
            colors[7] = System.Drawing.Color.FromArgb(255, 127, 127, 0);
            colors[8] = System.Drawing.Color.FromArgb(255, 0, 127, 127);

            colors[9] = System.Drawing.Color.FromArgb(255, 127, 255, 127);
            colors[10] = System.Drawing.Color.FromArgb(255, 127, 127, 255);
            colors[11] = System.Drawing.Color.FromArgb(255, 255, 127, 127);

            colors[12] = System.Drawing.Color.FromArgb(255, 127, 65, 0);
            colors[13] = System.Drawing.Color.FromArgb(255, 0, 65, 127);
            colors[14] = System.Drawing.Color.FromArgb(255, 127, 0, 65);
            colors[15] = System.Drawing.Color.FromArgb(255, 65, 0, 127);
            colors[16] = System.Drawing.Color.FromArgb(255, 0, 127, 65);
            colors[17] = System.Drawing.Color.FromArgb(255, 65, 127, 0);

            colors[18] = System.Drawing.Color.FromArgb(255, 127, 191, 0);
            colors[19] = System.Drawing.Color.FromArgb(255, 0, 191, 127);
            colors[20] = System.Drawing.Color.FromArgb(255, 127, 0, 191);
            colors[21] = System.Drawing.Color.FromArgb(255, 191, 0, 127);
            colors[22] = System.Drawing.Color.FromArgb(255, 0, 127, 191);
            colors[23] = System.Drawing.Color.FromArgb(255, 191, 127, 0);

            colors[24] = System.Drawing.Color.FromArgb(255, 255, 127, 0);
            colors[25] = System.Drawing.Color.FromArgb(255, 0, 127, 255);
            colors[26] = System.Drawing.Color.FromArgb(255, 127, 255, 0);
            colors[27] = System.Drawing.Color.FromArgb(255, 0, 255, 127);
            colors[28] = System.Drawing.Color.FromArgb(255, 127, 0, 255);
            colors[29] = System.Drawing.Color.FromArgb(255, 255, 0, 127);
        }

        private System.Drawing.Color[] colors;
    }
}
